package com.java110.mall.cart.storeOrderEngine;

import com.alibaba.fastjson.JSONObject;
import com.java110.core.factory.GenerateCodeFactory;
import com.java110.dto.vipUser.ShopVipAccountDto;
import com.java110.dto.vipUser.ShopVipLevelDto;
import com.java110.dto.vipUser.ShopVipUserDto;
import com.java110.dto.storeOrder.StoreOrderCartDto;
import com.java110.intf.acct.IShopVipAccountDetailInnerServiceSMO;
import com.java110.intf.acct.IShopVipAccountInnerServiceSMO;
import com.java110.intf.cart.IStoreOrderInnerServiceSMO;
import com.java110.intf.shop.IShopVipLevelInnerServiceSMO;
import com.java110.intf.shop.IShopVipUserInnerServiceSMO;
import com.java110.po.shop.ShopVipAccountPo;
import com.java110.po.shop.ShopVipAccountDetailPo;
import com.java110.po.storeOrder.StoreOrderPo;
import com.java110.po.storeOrder.StoreOrderCartPo;
import com.java110.utils.util.Assert;
import com.java110.utils.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @ClassName StoreOrderCartDiscount
 * @Description TODO
 * @Author wuxw
 * @Date 2022/5/21 12:10
 * @Version 1.0
 * add by wuxw 2022/5/21
 **/

@Component
public class StoreOrderCartDiscount {

    @Autowired
    private IStoreOrderInnerServiceSMO storeOrderInnerServiceSMOImpl;

    @Autowired
    private IShopVipUserInnerServiceSMO shopVipUserInnerServiceSMOImpl;
    @Autowired
    private IShopVipLevelInnerServiceSMO shopVipLevelInnerServiceSMOImpl;
    @Autowired
    private IShopVipAccountInnerServiceSMO shopVipAccountInnerServiceSMOImpl;
    @Autowired
    private IShopVipAccountDetailInnerServiceSMO shopVipAccountDetailInnerServiceSMOImpl;


    public void sendVipDiscount(JSONObject param, List<StoreOrderCartPo> storeOrderCartPos, StoreOrderPo storeOrderPo) {

        if (storeOrderCartPos == null || storeOrderCartPos.size() < 1) {
            return;
        }
        //9.0 计算积分和金币
        JSONObject vipuser = param.getJSONObject("vipUsers");
        if (null == vipuser || vipuser.size() < 1) {
            return;
        }

        if (!StringUtil.isNullOrNone(vipuser.getString("vipId"))) {
            saveOrderIntegral(vipuser, storeOrderCartPos.get(0).getShopId(), storeOrderCartPos.get(0).getStoreId(), storeOrderPo);
            StoreOrderCartDto storeOrderCartDto = new StoreOrderCartDto();
            storeOrderCartDto.setOrderId(storeOrderPo.getOrderId());
            storeOrderCartDto.setVipId(vipuser.getString("vipId"));
            storeOrderCartDto.setShopId(storeOrderCartPos.get(0).getShopId());
            storeOrderCartDto.setPayPrice(storeOrderPo.getPayPrice());
            storeOrderInnerServiceSMOImpl.saveOrderGoldCount(storeOrderCartDto);
        }

    }

    private void saveOrderIntegral(JSONObject vipuser, String shopId, String storeId, StoreOrderPo storeOrderPo) {

        double amount = Double.parseDouble(storeOrderPo.getPayPrice());
        //查询会员是否存在
        ShopVipUserDto shopVipUserDto = new ShopVipUserDto();
        shopVipUserDto.setShopId(shopId);
        shopVipUserDto.setVipId(vipuser.getString("vipId"));
        List<ShopVipUserDto> shopVipUserDtos = shopVipUserInnerServiceSMOImpl.queryShopVipUsers(shopVipUserDto);
        Assert.listOnlyOne(shopVipUserDtos, "会员不存在");

        ShopVipLevelDto shopVipLevelDto = new ShopVipLevelDto();
        shopVipLevelDto.setShopId(shopId);
        shopVipLevelDto.setVipLevelId(shopVipUserDtos.get(0).getVipLevel());
        List<ShopVipLevelDto> shopVipLevelDtos = shopVipLevelInnerServiceSMOImpl.queryShopVipLevels(shopVipLevelDto);
        Assert.listOnlyOne(shopVipLevelDtos, "会员等级不存在");

        double goldCostMoney = Double.parseDouble(shopVipLevelDtos.get(0).getGoldCostMoney());

        double goldCount = Math.floor(amount / goldCostMoney);
        //向下取整
        double integralCount = Math.floor(amount / Double.parseDouble(shopVipLevelDtos.get(0).getIntegralCostMoney()));
        ShopVipAccountDto shopVipAccountDto = new ShopVipAccountDto();
        shopVipAccountDto.setVipId(vipuser.getString("vipId"));
        shopVipAccountDto.setAcctType("2004");
        List<ShopVipAccountDto> shopVipAccountDtos = shopVipAccountInnerServiceSMOImpl.queryShopVipAccounts(shopVipAccountDto);
        if (shopVipAccountDtos == null || shopVipAccountDtos.size() < 1) {
            ShopVipAccountPo shopVipAccountPo = new ShopVipAccountPo();
            shopVipAccountPo.setVipAcctId(GenerateCodeFactory.getGeneratorId("11"));
            shopVipAccountPo.setAcctName("积分账户");
            shopVipAccountPo.setAmount("0");
            shopVipAccountPo.setShopId(shopId);
            shopVipAccountPo.setStoreId(storeId);
            shopVipAccountPo.setVipId(vipuser.getString("vipId"));
            shopVipAccountPo.setAcctType("2004");
            int flag = shopVipAccountInnerServiceSMOImpl.saveShopVipAccount(shopVipAccountPo);
            if (flag < 1) {
                throw new IllegalArgumentException("新增积分账户失败");
            }
            ShopVipAccountDto vipAccountDto = new ShopVipAccountDto();
            vipAccountDto.setVipAcctId(shopVipAccountPo.getVipAcctId());
            vipAccountDto.setStoreId(shopVipAccountPo.getStoreId());
            vipAccountDto.setShopId(shopVipAccountPo.getShopId());
            vipAccountDto.setAmount(shopVipAccountPo.getAmount());
            vipAccountDto.setAcctType(shopVipAccountPo.getAcctType());
            shopVipAccountDtos.add(vipAccountDto);

        }
        ShopVipAccountDetailPo shopVipAccountDetailPo = new ShopVipAccountDetailPo();
        shopVipAccountDetailPo.setVipAcctId(shopVipAccountDtos.get(0).getVipAcctId());
        shopVipAccountDetailPo.setShopId(shopVipAccountDtos.get(0).getShopId());
        shopVipAccountDetailPo.setOrderId(storeOrderPo.getOrderId());
        shopVipAccountDetailPo.setStoreId(shopVipAccountDtos.get(0).getStoreId());
        shopVipAccountDetailPo.setAmount(integralCount + "");
        shopVipAccountDetailPo.setRemark("收银台累计积分");
        int detaFlag = shopVipAccountInnerServiceSMOImpl.prestoreAccount(shopVipAccountDetailPo);
        if (detaFlag < 1) {
            throw new IllegalArgumentException("更新积分账户明细失败");
        }
    }


}
